<?php
/**
 * @Author    紫梦
 * @DateTime  2017-11-02
 * @copyright 响响租车
 * @version   1.0
 * @desc  
 */
namespace app\index\controller;
use app\common\model\Admin;
use app\common\model\Agency;
use app\common\model\Cars;
use app\common\model\CarsInfo;
use app\common\model\Inspect;
use app\common\model\Repair;
use app\common\model\Secure;
use think\Db;
use app\common\model\RolesMenu;
use app\common\model\NodeGrop;
use app\common\model\OffAbnormal;
use app\common\model\OffNormal;
use app\common\model\OffRecord;
use \Firebase\JWT\JWT;



class Index extends Base
{  

    // 主页
    public function index()
    {
        $admin = Admin::where('id',session('admin.id'))->find();

        $menu = NodeGrop::where(['pid'=>0])->where('id','in',session('admin.menu_id'))->select();
        $datum = [];
        foreach ($menu as $key => $value) {
           
            $nodeGrop = NodeGrop::where(['pid'=>$value['id']])->where('id','in',session('admin.menu_id'))->select();
            $datum[$key] = $value;
            $datum[$key]['children'] = $nodeGrop;
        }   

        $data['name'] = $admin->nick_name;
        $data['roles'] = $admin->roles->role_name;

        

        $key = md5('abcdefghijklmn123456789');
        $token = array(
            "id"=>session('admin.id')
        );
        $jwt = JWT::encode($token, $key);
        
        $this->assign('jwt',$jwt); 
        

        $this->assign('menu',$datum);

        $this->assign('data',$data);
        return $this->fetch();
    }

    //桌面
    public function welcome()
    {
        $data['last_ip'] = session('admin.last_ip');
        $data['last_login'] = date('Y-m-d H:i:s',session('admin.last_login'));
        $data['car_num'] = Cars::count();
        $data['secure'] = Secure::where(['notice'=>1,'warn'=>1])->count();
        $data['inspect'] = Inspect::scope('InspectNum', time()+25920000)->count();
        $data['repair'] = Repair::scope('Status')->count();
        $data['onLine'] = CarsInfo::where(['network_status'=>1])->count();
        $data['abnormal'] = OffAbnormal::count();
        $data['normal'] = OffNormal::count();
        $data['unInstall'] = $data['car_num'] -$data['onLine'] - $data['abnormal'] - $data['normal'];
        $this->assign('rele_name',['库存','分时租赁','长租','以租代购','企业用车','共享用车','公务用车','自测用车','销售']);
        if(session('admin.agency_id')==1){
            $data['agency'] = Agency::where('id',session('admin.agency_id'))->value('name');
            $relevance = Charts::relevance();
            $this->assign('relevance',$relevance);

            $business = Charts::business();
            $this->assign('business',$business);

            $usage= Charts::usage();
            $this->assign('usage',$usage);

            $agency = Charts::agency();
            $this->assign('agency',$agency);
        }else{
            
            $this->assign('relevance',null);
            $this->assign('agency',null);
            $this->assign('usage',null);
            $this->assign('business',null);
        }

        $this->assign('data',$data);

        $abnormal = Charts::abnormal();
        $repair = Charts::repair();
        $this->assign('abnormal',$abnormal);
        $this->assign('repair',$repair);
        return $this->fetch();
        
    }

   
    
}
